package com.dy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.entity.Order;
import com.dy.entity.OrderVo;
import com.dy.entity.SnowIdUtils;
import com.dy.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //mq回调函数
    RabbitTemplate.ConfirmCallback callback = (CorrelationData var1, boolean var2,String var3)->{
        if (var2){
            String orderSerial = var1.getId();
            Order order = new Order();
            order.setOrderState(3l);
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("order_serial",orderSerial);
            orderService.update(order,wrapper);
        }
    };

    @PostMapping("/insertOrder")
    public String insertOrder(@RequestBody OrderVo orderVo){
        //开启手动确认
        rabbitTemplate.setMandatory(true);
        //绑定回调函数
        rabbitTemplate.setConfirmCallback(callback);

        Order order = new Order();
        order.setOrderState(0l);
        order.setOrderNumber(orderVo.getOrderNum());
        order.setUserName(orderVo.getUserName());
        order.setOrderPrice(orderVo.getSalePrice()*orderVo.getOrderNum());
        String orderSerial = SnowIdUtils.uniqueLongHex();
        order.setOrderSerial(orderSerial);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        order.setOrderTime(df.format(new Date()));
        order.setCinameName(orderVo.getCinemaName());
        if (orderService.save(order)){
            CorrelationData correlation = new CorrelationData(orderSerial);
            System.out.println(order);
            rabbitTemplate.convertAndSend("dyDirectExchange","order.queue",order,correlation);
            return orderSerial;
        }
        return null;
    }

    @ApiOperation("抢票")
    @PostMapping("/shakedown")
    public String shakedown(@RequestBody OrderVo orderVo){
        Order order = new Order();
        order.setOrderState(0l);
        order.setOrderNumber(orderVo.getOrderNum());
        order.setUserName(orderVo.getUserName());
        order.setOrderPrice(orderVo.getSalePrice()*orderVo.getOrderNum());
        String orderSerial = SnowIdUtils.uniqueLongHex();
        order.setOrderSerial(orderSerial);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        order.setOrderTime(df.format(new Date()));
        order.setCinameName(orderVo.getCinemaName());
        System.out.println(order);
        rabbitTemplate.convertAndSend("shakedownExchange","shakedown.queue",order);
        return orderSerial;
    }

    @GetMapping("/selectOrder")
    public List<Order> selectOrder(){
        return orderService.list();
    }

    @GetMapping("/selectOrderSerial/{orderSerial}")
    public Long selectOrderSerial(@PathVariable("orderSerial")String orderSerial){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_serial",orderSerial);
        Order one = orderService.getOne(wrapper);
        return one.getOrderState();
    }

    @GetMapping("/updateOrderState/{orderSerial}")
    public boolean updateOrderState(@PathVariable("orderSerial")String orderSerial){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_serial",orderSerial);
        Order order = new Order();
        order.setOrderState(1l);
        return orderService.update(order,wrapper);
    }

    @PostMapping("/persistent")
    public boolean persistent(@RequestBody Order order){
        return orderService.save(order);
    }
}
